package com.common.config;

import com.common.controller.MyFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;

/**
 * @classDescription:
 * @author:xiayingjie
 * @createTime:16/7/15
 */
//实现ServeletContainer 默认tomcat启动会调用其实现类 替换web.xml(只支持servlet3.0容器)
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {


    //必须写死 没法用资源文件，初始化这个类的时候没有初始化资源文件
    public static String uploadDir;

    private long uploadSize=-1L;


    private long uploadAllSize=-1L;

    //---contextLoaderListener
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{AppConfig.class,DataSourceConfig.class,PropertiesConfig.class};
    }

    //dispatchServelt
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //设置utf-8编码过滤器
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);

        return new Filter[]{characterEncodingFilter};
    }
    //registration得到的是disparchservlet  如果uploadDir为null的话，默认在项目目录下 必须要加
    @Override
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {

        registration.setMultipartConfig(new MultipartConfigElement(uploadDir,uploadSize,uploadAllSize,0));

    }
}
